package com.paomi.client.activity.message;

import android.app.Dialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.paomi.client.R;
import com.paomi.client.activity.message.model.SystemMsgJson;
import com.paomi.client.base.BaseLCEActivity;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.fragment.massage.model.UnMyMsgBean;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.utils.Util;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by pc on 2018/11/9
 * Describe:消息列表页面
 * com.paomi.client.activity.reply
 */
public class MessageListActivity extends BaseLCEActivity<SystemMsgJson.SystemMsgBean, MessageListPresenter> {

    @BindView(R.id.message_list)
    RecyclerView messageList;
    @BindView(R.id.textview_title)
    TextView textview_title;
    @BindView(R.id.tv_all)
    TextView tv_all;
    Dialog cachedialog;
    private List<SystemMsgJson.SystemMsgBean> commentList = new ArrayList<>();
    private int pageNum=1;
    private boolean isRead = false;//是否已读
    @BindView(R.id.button_goback)
    TextView buttonGoback;

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.msg_system);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));
        button_goback.setTypeface(iconfont);
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_msg_list;
    }

    @NonNull
    @Override
    public MessageListPresenter createPresenter() {
        return new MessageListPresenter();
    }

    @NonNull
    @Override
    protected RecyclerView createRecycler() {
        return messageList;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_msg_list_del;
    }

    @Override
    protected void initEmpty(TextView empty) {
        empty.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.img_message_empty, 0, 0);
        empty.setText(getResources().getString(R.string.null_msg));
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        tv_all.setTypeface(iconfont);
        buttonGoback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isRead) {
                    Intent intent = new Intent();
                    intent.putExtra("num2", "1");
                    setResult(2006, intent);
                }
                finish();
            }
        });
        getPresenter().getNum();
    }

    @Override
    protected void convert(BaseViewHolder helper, SystemMsgJson.SystemMsgBean item) {
        TextView tvOrderAll = helper.itemView.findViewById(R.id.tv_order_all);
        tvOrderAll.setTypeface(iconfont);
        helper.setText(R.id.notify_msg, item.getTitle());
        helper.setText(R.id.notify_date, Util.getTimeString(item.getCtime() * 1000));
        helper.setText(R.id.notify_content, item.getContent());
        if (item.getIsRead() == 1) {// 0 未读 1 已读
            helper.itemView.findViewById(R.id.notify_get).setVisibility(View.GONE);
            helper.setVisible(R.id.btnRead, false);
        } else {
            helper.itemView.findViewById(R.id.notify_get).setVisibility(View.VISIBLE);
            helper.setVisible(R.id.btnRead, true);
        }
//        helper.setOnCheckedChangeListener(R.id.btnDelete, new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                getPresenter().clickReaded("" + item.getId());
//            }
//        });
        helper.setOnClickListener(R.id.btnRead, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPresenter().clickReaded(MessageListActivity.this,"" + item.getId());
            }
        });
        //删除
        helper.setOnClickListener(R.id.btnDelete, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPresenter().deleteReaded(MessageListActivity.this,"" + item.getId());
            }
        });
        helper.setOnClickListener(R.id.ll_item, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Util.toast(MessageListActivity.this, "122");
            }
        });
    }

    @Override
    protected void onItemClick(View view, SystemMsgJson.SystemMsgBean item) {

    }

    @Override
    public void setData(List<SystemMsgJson.SystemMsgBean> data, boolean pullToRefresh) {
        super.setData(data, pullToRefresh);
        commentList.clear();
        commentList.addAll(data);
        if (data.size() %Constants.PAGE_SIZE != 0) {//移除FooterView
            getmAdapterWrapper().setLoadItemVisibility(false);
        }else{
            getmAdapterWrapper().setLoadItemVisibility(true);
        }
        getmLoadMoreHelper().setLoadMoreFinish();
        getmAdapterWrapper().notifyDataSetChanged();
    }

    @OnClick(R.id.rl_all)
    void chooseAll() {
        if(cachedialog==null || !cachedialog.isShowing()){
            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
            View view = View.inflate(this, R.layout.layout_clear_cache, null);
            builder.setView(view);
            builder.setCancelable(true);
            TextView btn_clear = view.findViewById(R.id.btn_clear);
            TextView tv_title = view.findViewById(R.id.tv_title);
            TextView tv_msg = view.findViewById(R.id.tv_msg);
            TextView btn_success = view.findViewById(R.id.btn_success);
            tv_title.setText(getResources().getString(R.string.system_msg_title));
            tv_msg.setText(getResources().getString(R.string.system_msg_msg));
            cachedialog = builder.create();
            cachedialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            cachedialog.show();

            btn_clear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    cachedialog.dismiss();
                }
            });
            btn_success.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                doFollow();
                    cachedialog.dismiss();
                    getPresenter().clickReadedAll(MessageListActivity.this);
                }
            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe
    public void onEvent(Object result) {
        if (result instanceof UnMyMsgBean) {
            if (((UnMyMsgBean) result).returnCode.equals("0000")) {
                textview_title.setText(getResources().getString(R.string.msg_system) +
                        "(" + ((UnMyMsgBean) result).getReturnData().getMessageTotal() + ")");
            }
        }else if (result instanceof BaseJSON) {
            if (((BaseJSON) result).returnMessage == null) {
                return;
            }
            if (((BaseJSON) result).returnMessage.equals("删除成功")) {
                isRead=true;
                ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.delete_success));
            } else if (((BaseJSON) result).returnMessage.equals("全部已读")) {
                isRead=true;
                ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.message_all));
            } else if (((BaseJSON) result).returnMessage.equals("已读成功")) {
                isRead=true;
                ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.edit_read));
            }
        }
    }

    @Override
    public void onLoad() {
        super.onLoad();
        if(commentList.size()%Constants.PAGE_SIZE==0){
            pageNum++;
            getPresenter().loadData(pageNum, 1, false);
        }else{
            getmAdapterWrapper().setLoadItemVisibility(false);
            getmLoadMoreHelper().setLoadMoreFinish();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if(isRead) {
            Intent intent = new Intent();
            intent.putExtra("num2", "1");
            setResult(2006, intent);
        }
    }
}
